package server;

public class Statistics {
	private long dbPollTime;
	private long dbSendTime;
	
	private long pollTime;
	private long sendTime;
	private long pollQueries;
	private long sendQueries;
	private static final Statistics instance = new Statistics();
	
	public static Statistics getInstance(){
		return instance;
	}
	
	private Statistics(){
		dbPollTime = 0;
		dbSendTime = 0;
		pollTime = 0;
		sendTime = 0;
		pollQueries = 1;
		sendQueries = 1;
	}
	
	public synchronized void logDbSend(long queryTime) {
		// The accumulated time spent inserting entries in the database (only the actual database operation)
		dbSendTime += queryTime;
	}
	public synchronized void logDbPoll(long queryTime) {
		// The accumulated time spent extracting data from the database (only the actual database operation)
		dbPollTime += queryTime;
	}
	
	public synchronized void logPoll(long queryTime) {
		pollQueries++;
		// The time spent answering poll requests (including overhead)
		pollTime += queryTime;
	}
	
	public synchronized void logSend(long queryTime) {
		sendQueries++;
		// The time spent answering send requests (including overhead)
		sendTime += queryTime;
	}
		
	public synchronized String generateStatistics(){
		long totalQueries = pollQueries + sendQueries;
		long totalQueryTime = pollTime + sendTime;
		
		return  "Queries: " + totalQueries + "\n" +
				" Mean query time: " + totalQueryTime / totalQueries +"\n" +
				"Mean DbPoll time: " + dbPollTime / pollQueries + "\n" +
				"Mean DbSend time: " + dbSendTime / sendQueries +  "\n";
	}
}
